package com.yin.myproject.codeGenerate.factory;

import com.yin.myproject.codeGenerate.ColumnData;
import com.yin.myproject.codeGenerate.CreateBean;
import com.yin.myproject.codeGenerate.def.CodeResourceUtil;
import com.yin.myproject.codeGenerate.def.FreemarkerEngine;
import com.yin.myproject.util.StringUtils;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CodeGenerateFactory {

    private static String url;
    private static String username;
    private static String passWord;
    private static String databaseName;

    static {
        url = CodeResourceUtil.URL;
        username = CodeResourceUtil.USERNAME;
        passWord = CodeResourceUtil.PASSWORD;
        databaseName = CodeResourceUtil.DATABASE_NAME;
    }

    public CodeGenerateFactory() {
    }

    public static void codeGenerateByFTL(String tableName, String codeName, String keyType) {
        try {
            tableName = tableName == null ? "" : tableName;//.toUpperCase()
            System.out.println("----------------------------" + tableName + "持久化模板代码生成开始---------------------------");
            CreateBean createBean = new CreateBean();
            createBean.setMysqlInfo(url, username, passWord, databaseName);
            String className = createBean.getTablesNameToClassName(tableName);
            String daoName = className.substring(0, className.length() - 6) + "Dao";
            String lowerName = className.substring(0, 1).toLowerCase() + className.substring(1, className.length());
            String tableNameUpper = tableName;
            String tablesAsName = createBean.getTablesASName(tableName);
            if (StringUtils.isEmpty(codeName)) {
                Map<String, String> tableCommentMap = createBean.getTableCommentMap();
                codeName = (String) tableCommentMap.get(tableNameUpper);
                codeName = codeName == null ? "" : codeName.trim();
            }
            String getBestMatchedTables = CodeResourceUtil.getConfigInfo("getBestMatched_tables");
            getBestMatchedTables = getBestMatchedTables == null ? "" : getBestMatchedTables.trim();
            String getBestMatchedFlag = "N";
            if (getBestMatchedTables != null) {
                getBestMatchedFlag = getBestMatchedTables.contains(tableNameUpper) ? "Y" : "N";
            }
            String resourcePathSrc = CodeResourceUtil.getConfigInfo("resource_path_src");
            String classPathSrc = CodeResourceUtil.getConfigInfo("class_path_src");

            String sqlMapperPackage = CodeResourceUtil.getConfigInfo("sqlMapper_path");
            String entityPackage = CodeResourceUtil.getConfigInfo("entity_path");
            String servicePackage = CodeResourceUtil.getConfigInfo("service_path");
            String daoPackage = CodeResourceUtil.getConfigInfo("dao_path");
            String serviceImpPackage = CodeResourceUtil.getConfigInfo("service_imp_path");

            String sqlMapperPath = (new StringBuilder(String.valueOf(sqlMapperPackage.replace(".", "\\")))).append("\\")
                    .append(className).append("Mapper.xml").toString();
            String entityPath = (new StringBuilder(String.valueOf(entityPackage.replace(".", "\\")))).append("\\")
                    .append(className).append("Entity.java").toString();
            String daoPath = (new StringBuilder(String.valueOf(daoPackage.replace(".", "\\")))).append("\\")
                    .append(className).append("Mapper.java").toString();
            String servicePath = (new StringBuilder(String.valueOf(servicePackage.replace(".", "\\")))).append("\\")
                    .append(className).append("Dao.java").toString();
            String serviceImpPath = (new StringBuilder(String.valueOf(serviceImpPackage.replace(".", "\\")))).append("\\")
                    .append(className).append("DaoImpl.java").toString();
            String sqlMapperFlag = CodeResourceUtil.getConfigInfo("sqlMapper_flag");
            String domainFlag = CodeResourceUtil.getConfigInfo("entity_flag");
            String daoFlag = CodeResourceUtil.getConfigInfo("dao_flag");
            String serviceFlag = CodeResourceUtil.getConfigInfo("service_flag");
            String serviceImplFlag = CodeResourceUtil.getConfigInfo("service_imp_flag");

            Map<String, Object> sqlMap = createBean.getAutoCreateSql(tableName);

            List<ColumnData> columnDatas = createBean.getColumnDatas(tableName);
            List<ColumnData> columnKeyDatas = createBean.getColumnKeyDatas(columnDatas);
            String columnKeyParam = createBean.getColumnKeyParam(columnKeyDatas);
            String columeDataType = createBean.getColumeDataType(columnKeyDatas);
            String columnKeyUseParam = createBean.getColumnKeyUseParam(columnKeyDatas);
            String columnKeySort = createBean.getColumnKeySort(columnKeyDatas);
            String ignoreGenerateColumns = CodeResourceUtil.getConfigInfo("ignore_generate_columns");
            SimpleDateFormat dateformat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
            String nowDate = dateformat.format(new Date());
            System.out.println((new StringBuilder("开始生成时间:")).append(nowDate).toString());
            Map<String, Object> root = new HashMap<String, Object>();
            root.put("className", className);
            root.put("daoName",daoName);
            root.put("lowerName", lowerName);
            root.put("codeName", codeName);
            root.put("tableName", tableName);
            root.put("tableNameUpper", tableNameUpper);
            root.put("tablesAsName", tablesAsName);
            root.put("entityPackage", entityPackage);
            root.put("servicePackage", servicePackage);
            root.put("daoPackage", daoPackage);
            root.put("serviceImpPackage", serviceImpPackage);
            root.put("service_path", servicePath);
            root.put("dao_path", daoPath);
            root.put("keyType", keyType);
            root.put("nowDate", nowDate);
            root.put("feilds", createBean.getBeanFeilds(tableName, className));
            //root.put("queryfeilds", createBean.getQueryBeanFeilds(tableName, className));
            root.put("columnDatas", columnDatas);
            root.put("columnKeyDatas", columnKeyDatas);
            root.put("columnKeyParam", columnKeyParam);
            root.put("columeDataType", columeDataType);
            root.put("columnKeyUseParam", columnKeyUseParam);
            root.put("columnKeySort", columnKeySort);
            root.put("beanFieldDataTypes", createBean.getBeanFieldDataTypes());
            root.put("SQL", sqlMap);
            root.put("getBestMatchedFlag", getBestMatchedFlag);
            root.put("columnKeyString", columnKeySort.replace(",", ":"));
            root.put("ignoreGenerateColumns", ignoreGenerateColumns);
            Configuration cfg = new Configuration();
            String templateBasePath = (new StringBuilder(String.valueOf(getProjectPath()))).append(
                    CodeResourceUtil.getTEMPLATEPATH()).toString();
            cfg.setDirectoryForTemplateLoading(new File(templateBasePath));
            cfg.setObjectWrapper(new DefaultObjectWrapper());
            if ("Y".equals(sqlMapperFlag)) {
                FreemarkerEngine.createFileByFTL(cfg, root, "sqlMapper.ftl", resourcePathSrc, sqlMapperPath);
            }
            if ("Y".equals(domainFlag)) {
                FreemarkerEngine.createFileByFTL(cfg, root, "entityClass.ftl", classPathSrc, entityPath);
            }
            if ("Y".equals(daoFlag)) {
                FreemarkerEngine.createFileByFTL(cfg, root, "daoClass.ftl", classPathSrc, daoPath);
            }
            if ("Y".equals(serviceFlag)) {
                FreemarkerEngine.createFileByFTL(cfg, root, "daoClass.ftl", classPathSrc, daoPath);
            }
            if ("Y".equals(serviceImplFlag)) {
                FreemarkerEngine.createFileByFTL(cfg, root, "serviceImplClass.ftl", classPathSrc, serviceImpPath);
            }
            System.out.println("----------------------------" + tableNameUpper + "持久化模板代码生成完毕---------------------------");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    public static String getProjectPath() {
//		String path = (new StringBuilder(String.valueOf(System.getProperty("user.dir").replace("\\", "/"))))
//				.append("/").toString();
//		path += "target/classes/";
        String path = CodeGenerateFactory.class.getClassLoader().getResource("").getPath();
        System.out.println("模板路径：" + path);
        return path;
    }
}
